package com.shuqi.activity.viewport;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;

import com.shuqi.common.utils.Util;
import com.shuqi.controller.R;



public class CustomPopupWindow extends PopupWindow {

    private View mMenuView;
    private Context context;

    public CustomPopupWindow(final Activity context, View view) {
        super(context);
        this.context = context;
        mMenuView = view;

        int h = context.getWindowManager().getDefaultDisplay().getHeight();
        int w = context.getWindowManager().getDefaultDisplay().getWidth();
        // 设置按钮监听
        // popwindow显示时能监听按键setOnKeyListener
        view.setFocusableInTouchMode(true);
        // 设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(Util.dip2px(context, 150));
        // this.setWidth(w / 2 + 50);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 设置SelectPicPopupWindow弹出窗体的背景,不设置背景不能实现点击外围取消popMenu
        this.setBackgroundDrawable(dw);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
        mMenuView.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_MENU && event.getAction() == KeyEvent.ACTION_DOWN
                        && event.getRepeatCount() == 0) {
                    dismiss();
                }
                return true;
            }
        });
    }

    public CustomPopupWindow(final Activity context, View view, int width) {
        super(context);
        this.context = context;
        mMenuView = view;

        int h = context.getWindowManager().getDefaultDisplay().getHeight();
        int w = context.getWindowManager().getDefaultDisplay().getWidth();
        // 设置按钮监听
        // popwindow显示时能监听按键setOnKeyListener
        view.setFocusableInTouchMode(true);
        // 设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        if (width != ViewGroup.LayoutParams.FILL_PARENT
                && width != ViewGroup.LayoutParams.WRAP_CONTENT) {
            this.setWidth(Util.dip2px(context, width));
        } else {
            this.setWidth(width);
        }

        // this.setWidth(w / 2 + 50);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 设置SelectPicPopupWindow弹出窗体的背景,不设置背景不能实现点击外围取消popMenu
        this.setBackgroundDrawable(dw);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
        mMenuView.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_MENU && event.getAction() == KeyEvent.ACTION_DOWN
                        && event.getRepeatCount() == 0) {
                    dismiss();
                }
                return true;
            }
        });
    }



    public void showDown(View view) {
        int[] location = new int[2];
        view.getLocationOnScreen(location);

        showAsDropDown(view, -getWidth() / 2 + view.getWidth(), 10);
    }

    public void showLeft(View view) {
        int[] location = new int[2];
        view.getLocationOnScreen(location);

        showAsDropDown(view, -getWidth() + view.getWidth(), 10);
    }

    public void showRight(View view) {
        int[] location = new int[2];
        view.getLocationOnScreen(location);

        showAsDropDown(view, location[0] + Util.dip2px(context, 5), 0 - Util.dip2px(context, 7));
    }

}
